package com.persistence.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.model.OrdenProduccion;
import com.persistence.dao.DAOException;
import com.persistence.dao.DAOManagerMain;
import com.persistence.dao.interfaces.DAOOrdenProduccion;

public class MySQLDAOOrdenProduccion implements DAOOrdenProduccion {
	
	private OrdenProduccion ordenproduccion = null;
	
	
	public MySQLDAOOrdenProduccion() {
	}

	public int insert(OrdenProduccion ordenproduccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("insert into OrdenProduccion ("
					+ "Id_OrdProd, fechaAlta, fechaVigencia, Activo "
					+ "values (?, ?, ?, ?) ");

			stmt.setInt(1, ordenproduccion.getNroOrdProd());
			stmt.setString(2, ordenproduccion.getFechaAlta());
			stmt.setString(3, ordenproduccion.getFechaVigencia());
			stmt.setBoolean(4, ordenproduccion.isActivo());
			stmt.executeUpdate();
			
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);

		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int update(OrdenProduccion ordenproduccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("update OrdenProduccion " 
							+ "set Nombre = ?, Apellido = ?, Telefono = ?"
							+ " where id_OrdenProduccion = ? ");

         stmt.setString(1, ordenproduccion.getFechaAlta());
         stmt.setString(2, ordenproduccion.getFechaVigencia());
         stmt.setBoolean(3, ordenproduccion.isActivo());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int eliminate(OrdenProduccion ordenproduccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("delete from OrdenProduccion where id_OrdenProduccion = ? ");
			stmt.setInt(1, ordenproduccion.getNroOrdProd());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public ArrayList<OrdenProduccion> select() throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ArrayList<OrdenProduccion> ordenproduccions = new ArrayList<OrdenProduccion>();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("select * from OrdenProduccion");
			rs = stmt.executeQuery();

			while (rs.next()) {
				OrdenProduccion ordenproduccion = new OrdenProduccion();
				ordenproduccion.setNroOrdProd(rs.getInt("Id_OrdProd"));
				ordenproduccion.setFechaAlta(rs.getString("fechaAlta"));
				ordenproduccion.setFechaVigencia(rs.getString("fechaVigencia"));
				ordenproduccion.setActivo(rs.getBoolean("activo"));

				ordenproduccions.add(ordenproduccion);
			}

			return ordenproduccions;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public OrdenProduccion findById(int id) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ordenproduccion = new OrdenProduccion();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from OrdenProduccion where Id_OrdenProduccion = ?");
			stmt.setLong(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
			   ordenproduccion.setNroOrdProd(rs.getInt("Id_OrdProd"));
            ordenproduccion.setFechaAlta(rs.getString("fechaAlta"));
            ordenproduccion.setFechaVigencia(rs.getString("fechaVigencia"));
            ordenproduccion.setActivo(rs.getBoolean("activo"));
			}

			return ordenproduccion;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	@Override
	public boolean existInstance(OrdenProduccion ordenproduccion) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		boolean result = false;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from OrdenProduccion where UCASE(Nombre) = ?");

//			stmt.setString(1, ordenproduccion.getNombre().toUpperCase());
//			stmt.setString(2, ordenproduccion.getApellido().toUpperCase());
			rs = stmt.executeQuery();

			if (rs.next()) {
				result = true;
			}

			return result;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}
}
